package com.an.reflectandresource;


import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.PathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.WritableResource;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * 通过Spring的resource接口操作文件资源
 */
public class FileSourceExample {

   /* public static void main(String[] args){

        String filePath = "E:\\资料\\新建文本文档.txt";

        //shi使用系统文件路径方式加载文件
        WritableResource resource = new PathResource(filePath);

        //使用类路径加载文件
        Resource resource12 = new ClassPathResource("spring4.x.sql");

        //使用WriteAbleResource接口写资源文件
        try {
            OutputStream stream = resource.getOutputStream();
            stream.write("中国真好".getBytes());
            stream.close();

            //使用resource接口读取资源文件
            InputStream inputStream = resource.getInputStream();

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            int i = 0;
            while((i = inputStream.read()) != -1){
                baos.write(i);
            }
            System.out.println(baos.toString());
            System.out.println("文件名称：" + resource.getFilename());
            System.out.println("文件名称：" + resource12.getFilename());

        } catch (IOException e) {
            e.printStackTrace();
        }


    }*/
}
